﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using NJL.BLL;
using System.Text.RegularExpressions;

namespace NJL.WebManage.Users
{
    public partial class PersonManage : BasePage
    {
        PagedDataSource Pager;
        NJL_PERSON Bll = new NJL_PERSON();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                ViewState["INDEX"] = 0;

                ReBind();
            }
        }

        public void ReBind()
        {
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            DataSet set = Bll.GetList("");
            int index = Convert.ToInt32(ViewState["INDEX"]);
            Pager = new PagedDataSource();
            Pager.DataSource = Bll.GetList("").Tables[0].DefaultView;
            Pager.AllowPaging = true;
            Pager.PageSize = 15;
            Pager.CurrentPageIndex = index;

            this.litAllPage.Text = Pager.PageCount.ToString();
            this.lit.Text = Pager.Count.ToString();  
            this.litCurrentPage.Text = (Pager.CurrentPageIndex + 1).ToString();
            this.lbtnFirst.Visible = !Pager.IsFirstPage;
            this.lbtnEnd.Visible = !Pager.IsLastPage;
            this.lbtnPrev.Visible = index - 1 > Pager.FirstIndexInPage;
            this.lbtnNext.Visible = index + 1 < Pager.PageCount;

            ViewState["ENDINDEX"] = Pager.Count;        
      
            PersonRepeater.DataSource = Pager;
            PersonRepeater.DataBind();
        }

        protected void PersonRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DELETE")
            {
                string id = e.CommandArgument.ToString();
                if (Bll.Delete(id))
                {
                    ReBind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }

        protected void FirstIndex_Click(object sender, EventArgs e)
        {
            ViewState["INDEX"] = "0";
        }

        protected void PreviewIndex_Click(object sender, EventArgs e)
        {
            ViewState["INDEX"] = Convert.ToInt32(ViewState["pager"]) - 1;
            ReBind();
        }

        protected void NextIndex_Click(object sender, EventArgs e)
        {
            ViewState["INDEX"] = Convert.ToInt32(ViewState["pager"]) + 1;
            ReBind();
        }

        protected void EndIndex_Click(object sender, EventArgs e)
        {
            ViewState["INDEX"] = Convert.ToInt32(ViewState["ENDINDEX"]);
            ReBind();
        }

        protected void PersonRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal literal = e.Item.FindControl("RowIndex") as Literal;
            literal.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["INDEX"]) + 1) * 15 - 15).ToString();
        }

        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                ReBind();
            }
        }
    }
}